package com.tm.util;

import java.util.Map;

import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.lang.StringUtils;


public class HttpRemoteCall {
    
    
    /**
     * 建立请求，以模拟远程HTTP的POST请求方式构造并获取处理结果
     * 如果接口中没有上传文件参数，那么strParaFileName与strFilePath设置为空值
     * 如：buildRequest("", "",sParaTemp)
     * @param strParaFileName 文件类型的参数名
     * @param strFilePath 文件路径
     * @param sParaTemp 请求参数数组
     * @return 支付宝处理结果
     * @throws Exception
     */
    public static String buildRequest(String url, Map<String, String> sParaTemp, String charset, String strParaFileName, String strFilePath) throws Exception {
        //待请求参数数组
        //Map<String, String> sPara = buildRequestPara(sParaTemp);

        HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance();

        HttpRequest request = new HttpRequest(HttpResultType.BYTES);
        //设置编码集
        request.setCharset(charset);
        
    	request.setParameters(generatNameValuePair(sParaTemp));
        
        request.setUrl(url);

        HttpResponse response = httpProtocolHandler.execute(request,strParaFileName,strFilePath);
        
        if (response == null) {
            return null;
        }
        
        String strResult = response.getStringResult();

        return strResult;
    }
    
    /**
     *  建立请求，以设定METHOD方式请求方式构造并获取处理结果
     * @param url
     * @param sParaTemp
     * @param charset
     * @param strParaFileName
     * @param strFilePath
     * @param method
     * @return
     * @throws Exception
     */
    public static String buildRequest(String url, Map<String, String> sParaTemp, String charset, String strParaFileName, String strFilePath, String method) throws Exception {
        //待请求参数数组
        //Map<String, String> sPara = buildRequestPara(sParaTemp);

        HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance();
        
        HttpResponse response  = null;
        String strResult ="";

        HttpRequest request = new HttpRequest(HttpResultType.BYTES);
        //设置编码集
        request.setCharset(charset);
        
        //设定请求方式：GET/POST
        if(method != null && !StringUtils.isBlank(method)){
        	request.setMethod(method);
        }

        //根据方式不同设定参数
        if(HttpRequest.METHOD_GET.equals(request.getMethod())){
        	request.setQueryStringPair(generatNameValuePair(sParaTemp));
        }else{
        	request.setParameters(generatNameValuePair(sParaTemp));
        }
        
      //设定URL
        request.setUrl(url);

        response = httpProtocolHandler.execute(request,strParaFileName,strFilePath);
        
        if (response == null) {
            return null;
        }
        
        strResult = response.getStringResult();

        return strResult;
    }
    
    /**
     *  建立请求，以设定METHOD方式请求方式构造并获取处理结果
     * @param url
     * @param sParaTemp
     * @param charset
     * @param strParaFileName
     * @param strFilePath
     * @param method
     * @return
     * @throws Exception
     */
    public static byte[] buildRequestByteResult(String url, Map<String, String> sParaTemp, String charset, String strParaFileName, String strFilePath, String method) throws Exception {
        //待请求参数数组
        //Map<String, String> sPara = buildRequestPara(sParaTemp);

        HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance();
        
        HttpResponse response  = null;
        //byte[] strResult ="";

        HttpRequest request = new HttpRequest(HttpResultType.BYTES);
        //设置编码集
        request.setCharset(charset);
        
        //设定请求方式：GET/POST
        if(method != null && !StringUtils.isBlank(method)){
        	request.setMethod(method);
        }

        //根据方式不同设定参数
        if(HttpRequest.METHOD_GET.equals(request.getMethod())){
        	request.setQueryStringPair(generatNameValuePair(sParaTemp));
        }else{
        	request.setParameters(generatNameValuePair(sParaTemp));
        }
        
      //设定URL
        request.setUrl(url);

        response = httpProtocolHandler.execute(request,strParaFileName,strFilePath);
        
        if (response == null) {
            return null;
        }
        
        //byte[] strResult = null;
        
        byte[] strResult = response.getByteResult();

        return strResult;
    }

    /**
     * MAP类型数组转换成NameValuePair类型
     * @param properties  MAP类型数组
     * @return NameValuePair类型数组
     */
    private static NameValuePair[] generatNameValuePair(Map<String, String> properties) {
        NameValuePair[] nameValuePair = new NameValuePair[properties.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            nameValuePair[i++] = new NameValuePair(entry.getKey(), entry.getValue());
        }

        return nameValuePair;
    }
}
